#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>
#define TNUM 4
typedef void (*func_t)(const std::string &name, pthread_mutex_t *pmtx, pthread_cond_t *pcond); // void*表示返回值类型是一个void*类型
using namespace std;
volatile bool quit = false;
class ThreadData
{
public:
    ThreadData(const std::string &name, func_t func, pthread_mutex_t *pmtx, pthread_cond_t *pcond)
        : name_(name), func_(func), pmtx_(pmtx), pcond_(pcond)
    {
    }
    std::string name_;
    func_t func_;
    pthread_mutex_t *pmtx_;
    pthread_cond_t *pcond_;
};
void func1(const std::string &name, pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        cout << name << "在等待之前输出quit的结果：" << quit << endl;
        pthread_cond_wait(pcond, pmtx); // 执行到这里，默认该线程会阻塞，该线程的pcb会被挂起
        cout << name << "running..." << endl;
        pthread_mutex_unlock(pmtx);
    }
}
void func2(const std::string &name, pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        cout << name << "在等待之前输出quit的结果：" << quit << endl;
        pthread_cond_wait(pcond, pmtx); // 执行到这里，默认该线程会阻塞，该线程的pcb会被挂起
        cout << name << "running..." << endl;
        pthread_mutex_unlock(pmtx);
    }
}
void func3(const std::string &name, pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        cout << name << "在等待之前输出quit的结果：" << quit << endl;
        pthread_cond_wait(pcond, pmtx); // 执行到这里，默认该线程会阻塞，该线程的pcb会被挂起
        cout << name << "running..." << endl;
        pthread_mutex_unlock(pmtx);
    }
}
void func4(const std::string &name, pthread_mutex_t *pmtx, pthread_cond_t *pcond)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        cout << name << "在等待之前输出quit的结果：" << quit << endl;
        pthread_cond_wait(pcond, pmtx); // 执行到这里，默认该线程会阻塞，该线程的pcb会被挂起
        cout << name << "running..." << endl;
        pthread_mutex_unlock(pmtx);
    }
}
void *Entry(void *args)
{
    ThreadData *td = (ThreadData *)args;         // 这里的td是在每一个线程自己的栈空间定义的
    td->func_(td->name_, td->pmtx_, td->pcond_); // 调用对应的函数
    delete td;
    return nullptr;
}
int main()
{
    pthread_mutex_t mtx;               // 申请一个锁
    pthread_cond_t cond;               // 申请一个条件变量
    pthread_mutex_init(&mtx, nullptr); // 初始化只有一把锁，把这把锁分别传进不同线程的td罢了
    pthread_cond_init(&cond, nullptr); // 一个条件变量被所有线程通过ThreadData类型的td变量共享

    pthread_t tids[TNUM];
    func_t funcs[TNUM] = {func1, func2, func3, func4};
    for (int i = 0; i < TNUM; i++)
    {
        string name = "Thread ";
        name += to_string(i + 1);
        ThreadData *td = new ThreadData(name, funcs[i], &mtx, &cond);
        pthread_create(tids + i, nullptr, Entry, (void *)td); // 创建线程
    }
    sleep(5);
    // 每隔1秒，唤醒一个线程
    int cnt = 10;
    while (cnt)
    {
        cout << "resume thread ,run code.." << cnt-- << endl;
        pthread_cond_signal(&cond);
        sleep(1);
    }
    cout << "线程同步成功1" << endl;
    quit = true;
    cout << "线程同步成功2" << endl;
    pthread_cond_broadcast(&cond);
    for (int i = 0; i < TNUM; i++)
    {
        pthread_join(tids[i], nullptr);
        cout << "Thread: " << tids[i] << "quit" << endl;
    }
    pthread_mutex_destroy(&mtx);
    pthread_cond_destroy(&cond);
}